package cn.onlineclass.controller;

import cn.onlineclass.dao.OMajorMapper;
import cn.onlineclass.entity.OMajor;
import cn.onlineclass.service.OMajorService;
import cn.onlineclass.util.AjaxPage;
import cn.onlineclass.util.AjaxResult;
import cn.onlineclass.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class OMajorController {

    public AjaxPage search(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int pageNo = StringUtil.toInt(request.getParameter("pageNo"), 1);
//        boolean inuse = StringUtil.isTrue(request.getParameter("inuse"));
//        long ratationid = Long.parseLong(StringUtil.trim(request.getParameter("ratationid")));
        AjaxPage p = OMajorService.INST.search(pageNo, 10, true,1);
//        String str = JSON.toJSONStringWithDateFormat(p, "yyyy-MM-dd", SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty,
//                SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNonStringValueAsString);
//        response.getWriter().print(str);
        return p;
    }

    public AjaxResult add(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
//        String name = request.getParameter("name");
//        boolean inuse = Boolean.parseBoolean(request.getParameter("inuse"));
//        String description = request.getParameter("description");
//        long ratationid = Long.parseLong(request.getParameter("ratationid"));
//        OMajor oMajor = new OMajor(name,inuse,description,ratationid);
        OMajor oMajor = new OMajor("test", true, "test", 1);
        int add = OMajorService.INST.add(oMajor);
        if (add > 0) {
            return AjaxResult.success("保存成功");
        }
        return AjaxResult.error("保存失败");
    }

    public AjaxResult toupdate(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
//        long id = Long.parseLong(request.getParameter("id"));
        long id = 1;
        OMajor oMajor = OMajorService.INST.get(id);
        if (oMajor == null) {
            return AjaxResult.error("读取失败");
        }
        System.out.println(AjaxResult.success(oMajor + "************88"));
        return AjaxResult.success(oMajor);
    }

    public AjaxResult update(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        //        String name = request.getParameter("name");
//        boolean inuse = Boolean.parseBoolean(request.getParameter("inuse"));
//        String description = request.getParameter("description");
//        long ratationid = Long.parseLong(request.getParameter("ratationid"));
//        OMajor oMajor = new OMajor(name,inuse,description,ratationid);
        OMajor oMajor = new OMajor(6, "11111", true, "test666", 1);
        int update = OMajorService.INST.update(oMajor);
        if (update == 0) {
            return AjaxResult.error("修改失败");
        }
        return AjaxResult.success("修改成功");
    }

    public AjaxResult del(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        //        String name = request.getParameter("name");
//        boolean inuse = Boolean.parseBoolean(request.getParameter("inuse"));
//        String description = request.getParameter("description");
//        long ratationid = Long.parseLong(request.getParameter("ratationid"));
//        OMajor oMajor = new OMajor(name,inuse,description,ratationid);
        OMajor oMajor = new OMajor(6,  false);
        int update = OMajorService.INST.del(oMajor);
        if (update == 1) {
            return AjaxResult.error("删除成功");
        }
        return AjaxResult.success("删除失败");
    }
}
